package exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import exam.bean.ProjectInfo;
import exam.bean.UserInfo;
import exam.bean.UtilFiles;
import exam.dao.ProjectInfoDao;
import exam.dao.UserInfoDao;
import exam.dao.UtilFileDao;
import exam.service.IProjectInfoService;
import exam.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectInfoServiceImpl implements IProjectInfoService {

    @Autowired
    ProjectInfoDao projectInfoDao;

    @Autowired
    UtilFileDao utilFileDao;


    @Autowired
    UserInfoDao userInfoDao;


    @Override
    public int insertInfo(ProjectInfo projectInfo,String foldPath) {
        projectInfo.setEnterTime(new Date());
        projectInfo.setFlag(0);
        //初始余额为项目总金额
        projectInfo.setRemainPrice(projectInfo.getTotalPrice());
        int inInfo = projectInfoDao.insertInfo(projectInfo);
        int fileRe = 0;
        if(inInfo > 0 && projectInfo.getUtilFiles() != null){
            for (MultipartFile file:projectInfo.getUtilFiles()) {
                UtilFiles files = new UtilFiles();
                files.setDataId(projectInfo.getId());
                files.setUtilName(FileUtil.upLoadFileSingle(file,foldPath));
                files.setUtilAddress(foldPath);
                files.setUtilSrcName(file.getOriginalFilename());
                files.setUtilFileType(file.getContentType());
                files.setUtilType(1);
                fileRe = utilFileDao.insertInfo(files);
            }
        }
        return inInfo + fileRe ;
    }

    @Override
    public int deleteInfo(int id) {
        return projectInfoDao.deleteInfo(id);
    }

    @Override
    public int updateInfo(ProjectInfo projectInfo,String upLoadPath) {
        //多文件上传
        Integer fileRe = 0;
        if(projectInfo.getUtilFiles() != null){
            for (MultipartFile file:projectInfo.getUtilFiles()) {
                UtilFiles files = new UtilFiles();
                files.setDataId(projectInfo.getId());
                files.setUtilName(FileUtil.upLoadFileSingle(file,upLoadPath));
                files.setUtilAddress(upLoadPath);
                files.setUtilSrcName(file.getOriginalFilename());
                files.setUtilFileType(file.getContentType());
                files.setUtilType(1);
                fileRe = utilFileDao.insertInfo(files);
            }
        }
        return projectInfoDao.updateInfo(projectInfo) + fileRe;
    }

    @Override
    public PageInfo<ProjectInfo> selectInfo(int pageNum, int pageSize,Map<String,Object> queryMap) {

        UserInfo userInfo = getCurrentUserInfo();
        //查询属于自己的数据 ---> 管理员除外
        if(userInfo.getUserGroupId() != 1 ){
            queryMap.put("enterUserId",userInfo.getId());
        }

        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(projectInfoDao.selectInfo(queryMap));
    }

    @Override
    public ProjectInfo selectInfoSingle(Map<String, Object> map) {

        UserInfo userInfo = getCurrentUserInfo();
        //查询属于自己的数据 ---> 管理员除外
        if(userInfo.getUserGroupId() != 1 ){
            map.put("enterUserId",userInfo.getId());
        }

//        附件查询
        ProjectInfo projectInfo = projectInfoDao.selectInfo(map).get(0);
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("utilType",1);
        queryMap.put("dataId",map.get("id"));
        projectInfo.setFiles(utilFileDao.selectInfo(queryMap));
        //将转义字符转化为HTML字符
        String htmlStr = projectInfo.getSpecifications();
        if(htmlStr != null){
            projectInfo.setSpecifications(HtmlUtils.htmlUnescape(htmlStr));
        }

        return projectInfo;
    }

    @Override
    public List<ProjectInfo> projectList() {
        Map<String,Object> queryMap = new HashMap<String,Object>();
        //获取只属于当前用户的项目
        UserInfo userInfo = getCurrentUserInfo();
        if(userInfo.getUserGroupId() != 1 ){
            queryMap.put("enterUserId",userInfo.getId());
        }
        return projectInfoDao.selectInfo(queryMap);
    }



    public UserInfo getCurrentUserInfo(){
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

        Map<String, Object> map = new HashMap<>();
        map.put("name",authenticationToken.getPrincipal());
        return userInfoDao.selectInfo(map).get(0);
    }

}
